*******************************************************************************
* Load candidates data for bullets
*******************************************************************************

use dataset_candidates, clear

keep if state == "California" & !mi(ideology)

*******************************************************************************
* Make a unique candidate-year id
*******************************************************************************

replace candidate = candidate + string(cycle)

*******************************************************************************
* Reshape to bullet level
*******************************************************************************

keep candidate ideology1 ideology2 ideology3 text* goldtext*

encode candidate, gen(id)
reshape i id
reshape j bullet
reshape xi candidate
reshape xij ideology text goldtext
reshape long

*******************************************************************************
* Drop gold bullets
*******************************************************************************

drop if goldtext == 1

*******************************************************************************
* Edit some text that causes formatting issues
*******************************************************************************

replace text = subinstr(text, "[name]", "", 10)
replace text = substr(text, 2, length(text)) if substr(text, 1, 1) == "-"
replace text = substr(text, 2, length(text)) if substr(text, 1, 1) == " "
replace text = lower(text)

*******************************************************************************
* Label most extreme terms and keep only those terms
*******************************************************************************

sort ideology text
gen lib = _n <= 10
gsort - ideology + text
gen con = _n <= 10
keep if lib == 1 | con == 1

*******************************************************************************
* Make two columns
*******************************************************************************

preserve
	keep if lib == 1
	gen rank = _n
	sort rank
	rename text term_lib
	rename ideology score_lib
	save tmp, replace
restore

keep if con == 1
gen rank = _n
sort rank
rename text term_con
rename ideology score_con
merge 1:1 rank using tmp
erase tmp.dta

*******************************************************************************
* Format for table output
*******************************************************************************

keep term_lib term_con
order term_lib

replace term_lib = subinstr(term_lib, "&", "\&", 10)
replace term_lib = subinstr(term_lib, "%", "\%", 10)

foreach x in lib con {
	replace term_`x' = subinstr(term_`x', "&", "and", 10)
	replace term_`x'= subinstr(term_`x', "%", "\%", 10)
	replace term_`x' = subinstr(term_`x', "#", "\#", 10)
	//replace term_`x' = subinstr(term_`x', "[", "\[", 10)
	//replace term_`x' = subinstr(term_`x', "]", "\]", 10)
	//local c 45
	//replace term_`x' = substr(term_`x', 1, `c') + "..." if ///
	//	length(term_`x') > `c'
	replace term_`x' = " \truncate{\linewidth}{" + term_`x' + "} "
	replace term_`x' = subinstr(term_`x', "Quote", "", 10)
	replace term_`x' = subinstr(term_`x', "quote", "", 10)
}

*******************************************************************************
* Output table
*******************************************************************************

list

#delimit;

listtex 
	using "_output/table2_a.tex"
	,
		replace 
		type 
		rstyle(tabular) 	
		head(
			\begin{tabular}{
				|p{0.5\textwidth}|
				p{0.5\textwidth}|
			}
			\hline
			Progressive
			& 
			Conservative
			\\
			\hline
		)
		foot("\hline\end{tabular}")
		;
		
#delimit cr

*******************************************************************************
* Load the web site terms data
*******************************************************************************

use dataset_terms, clear

*******************************************************************************
* Label most extreme terms and keep only those terms
*******************************************************************************

sort score
gen lib = _n <= 26
gsort - score
gen con = _n <= 26
keep if lib == 1 | con == 1

*******************************************************************************
* Make two columns
*******************************************************************************

preserve
	keep if lib == 1
	sort term
	gen rank = _n
	rename term term_lib
	rename score score_lib
	save tmp, replace
restore

keep if con == 1
sort term
gen rank = _n
rename term term_con
rename score score_con
merge 1:1 rank using tmp
erase tmp.dta

keep term*

order term_lib

*******************************************************************************
* Make four columns
*******************************************************************************

preserve
	keep if _n <= 13
	gen index = _n
	rename term_lib term_lib1
	rename term_con term_con1
	save tmp, replace
restore

keep if _n > 13
gen index = _n
merge 1:1 index using tmp
erase tmp.dta
drop index _m

/*
*******************************************************************************
* Combine all terms into one list
* Thanks: [https://www.statalist.org/forums/forum/general-stata-discussion/
* general/1295254-vertical-concat-of-string-observations]
*******************************************************************************

foreach x in lib con {
	gen block = missing(term_`x'[_n-1])
	replace block = sum(block)

	sort block, stable
	by block : gen term_`x'_all = term_`x' if _n == 1
	by block : replace term_`x'_all = term_`x'_all[_n-1] + ", " + ///
		term_`x' if _n > 1
	drop block term_`x'
}
keep if _n == _N
*/

*******************************************************************************
* Formatting
*******************************************************************************

foreach i in _lib _con _lib1 _con1 {
	replace term`i' = subinstr(term`i', "_", "\_", 1000)
}

order term_lib1 term_lib term_con1 term_con

*******************************************************************************
* Export
*******************************************************************************

#delimit;

listtex 
	using "_output/table2_b.tex"
	,
		replace 
		type 
		rstyle(tabular) 	
		head(
			\begin{tabular}{
				|p{0.24\textwidth}
				p{0.24\textwidth}|
				p{0.24\textwidth}
				p{0.23\textwidth}|				
			}			
			\hline
			Progressive &
			&
			Conservative &
			\\
			\hline
		)
		foot("\hline\end{tabular}")
		;
		
#delimit cr

*******************************************************************************
* End
*******************************************************************************
